package com.jie.service.impl;

import com.jie.constants.RedisCountConst;
import com.jie.dto.ActivityDTO;
import com.jie.dto.ActivityHotDTO;
import com.jie.entity.Activity;
import com.jie.enums.ZoneEnum;
import com.jie.utils.LocalDateTimeUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author: 杰
 * @Project :net-disk
 * @DateTime: 2024/4/26 16:15
 **/
@Component
public class HotRedisCountServiceImpl {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 对活动热门数据进行统计 按日 周 月来排行
     * @param activity 活动信息
     */
    public void activityHotCount(Activity activity){
        //获取当天日期
        String dateTimeDay = LocalDateTimeUtil.getDateTimeDay();
        ZSetOperations zSet = redisTemplate.opsForZSet();
        //添加信息
        zSet.incrementScore(dateTimeDay+RedisCountConst.HOT_ACTIVITY_COUNT_LIST,activity,1);
    }

    /**
     * 获取活动排行的热门数据
     */
    public List<ActivityHotDTO> getActivityHotCount(){
        //获取当天日期
        List<ActivityHotDTO> res=new ArrayList<>();
        String dateTimeDay = LocalDateTimeUtil.getDateTimeDay();
        ZSetOperations zset = redisTemplate.opsForZSet();
        Set<Activity> set = zset.rangeByScore(dateTimeDay + RedisCountConst.HOT_ACTIVITY_COUNT_LIST, 0, 10);
        set.forEach(e->{
            Double score = zset.score(dateTimeDay + RedisCountConst.HOT_ACTIVITY_COUNT_LIST, e);
            res.add(new ActivityHotDTO(e,score.intValue()));
        });
        return res;
    }

}
